// package-store/pages/store-earning/index.js
import { UserInfoUpdateMixins, ManageTargetInfoUpdateMixins } from '../../../behaviors/page';
import { PACKAGE_STORE_PAGE } from '../../../constants/route';
import { WITHDRAW_TYPE } from '../../../constants/common';
import { navTo, showLoading, hideLoading } from '../../../common/env';
import { getStoreInfo } from '../../../api/sto';
let loaded = false;

Page({
    mixins: [UserInfoUpdateMixins, ManageTargetInfoUpdateMixins],
    data: {
        settlementInfo: {},
    },
    onLoad(options) {
    },
    onShow() {
        this._handleGetDetail();
    },
    async _handleGetDetail() {
        !loaded && showLoading('加载中...');
        const { earningStats } = await getStoreInfo();
        this.setData({ settlementInfo: earningStats });
        if (!loaded) hideLoading(); loaded = true;
    },
    handleNavToWithdraw() {
        const { availableAmount, withdrawingAmount } = this.data.settlementInfo;
        navTo(PACKAGE_STORE_PAGE['storeWithdraw'], {
            type: WITHDRAW_TYPE.bank,
            amount: availableAmount - withdrawingAmount > 0 ? availableAmount - withdrawingAmount : 0,            
        });
    },
    handleNavToEarningRecord() {
        navTo(PACKAGE_STORE_PAGE['storeEarningRecord']);
    },
    handleNavToWithdrawRecord() {
        navTo(PACKAGE_STORE_PAGE['storeWithdrawRecord']);
    },
})
